package com.ywt.cda.access;

import javax.servlet.http.HttpServletRequest;

import org.apache.velocity.tools.view.context.ViewContext;

import com.ywt.cda.domain.UserAgent;
import com.ywt.cda.enums.FunctionsEnum;

public class AccessVMTool
{

	private UserAgent userAgent;
	
	public void init(Object obj) {
		if(!(obj instanceof ViewContext)) {
			throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
		}
		ViewContext viewContext = (ViewContext) obj;
		HttpServletRequest request = viewContext.getRequest();
		userAgent = (UserAgent) request.getAttribute(UserAgent.USER_AGENT_IN_REQUEST);
		if(userAgent.getAppId() == null || userAgent.getLoginName() == null) {
			throw new IllegalStateException("userAgent not find.");
		}
	}
	
	public boolean has(String functionName) {
		FunctionsEnum e = FunctionsEnum.valueOf(functionName);
		if(e == null) {
			throw new IllegalArgumentException("unknown function name:" + functionName);
		}
		return this.userAgent.haveFunction(e);
	}
}
